import time
import json
import argparse
import base64
from PIL import Image
from io import BytesIO
from datetime import datetime

# 视频分析
def video_ai():
    parser = argparse.ArgumentParser()
    parser.add_argument('--data', type=str, default = None)
    args = parser.parse_args()
    json_data = json.loads(args.data)
    # 设备号
    number = json_data.get('device_number')
    # 报警类型名称
    alarmTypeName = ""
    # 报警类型（算法传过来的）
    alarmType = json_data.get('alarm_type')
    # 报警时间
    #alarmTime = json_data.get('alarm_time')
    alarmTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    # 解析图片信息
    # decoded_image_data = base64.b64decode(json_data.get('base64img'))
    # image = Image.open(BytesIO(decoded_image_data))
    # image_name = int(time.time() * 1000)
    # 保存图像到文件
    # path = '/upload/images/'+str(image_name)+".jpg"
    # image.save('/mnt/work/www'+path)
    # if alarmType == '1':
    #     alarmTypeName = '区域入侵'


    # 报警数据
    json_alarm_data = {}
    # 返回数据
    data_json = {}

    json_alarm_data['alarmType'] = alarmType
    json_alarm_data['url'] = json_data.get('base64img')
    json_alarm_data['alarmTypeName'] = alarmType
    json_alarm_data['content'] = alarmType
    json_alarm_data['dataType'] = 'alarm'
    data_json["alarm"] = json_alarm_data

    data_json['number'] = number
    data_json['time'] = alarmTime
    return data_json


if __name__ == '__main__':
    print(video_ai())
